<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;
use app\common\request\Curl;
use app\common\request\ClientLai;
use app\common\model\CarsInfo;
use app\common\model\CarsTemp;

class Test extends Command
{
	protected function configure()
    {
        $this->setName('test')->setDescription('test');
    }

    protected function execute(Input $input, Output $output)
    {   
        $this->test();

        $output->writeln('success');
    }

/*    public function offCars(){
        try{

            $data = Db::name('cars_info')->where('network_status',null)->select();
        }catch(\Exception $e){
            Log::record('查找network_status为null的车辆'.$e->getMessage(),'error');
            return [];
        }
        
        return $data;
    }

    public function offTime(){
        $data = Db::name('cars_info')->where('network_status','<>',1)->column('car_unique_id');

        $time = time()*1000-3600000;
        try{

           $param = Db::name('cars_info')->where('car_unique_id','in',$data)->where('machine_report_time','<',$time)->select();

        }catch(\Exception $e){

           Log::record('查找离线1小时以上的车辆'.$e->getMessage(),'error');
           return []; 
        }

        return $param; 

    }

    public function seek($param){
        foreach ($param as $key => $value) {
            $cars = Db::name('off_record')->where(['car_unique_id'=>$value['car_unique_id']])->value('car_unique_id');

            if($cars){
                $time = time()*1000-3600000*7;

                $data = Db::name('off_record')->where(['car_unique_id'=>$cars])->where('create_time','<',$time)->find();

                if($data){
                    $this->renewal($data['car_unique_id'],$data); 
                }  
            }else{
               $this->renewal($value['car_unique_id'],$value);
            }   
        }    
    }

    public function renewal($data,$value){
        $dataum = $this->renovate($data,$value);
        if($dataum){
            $data_write = Db::name('off_write')->where(['car_unique_id'=>$data])->find();
            $model = new OffWrite;
            if($data_write){
                try {

                    $model->allowField(true)->save($dataum,['car_unique_id'=>$data]);
                     
                } catch (\Exception $e) {
                    Log::record('更新off_write表数据'.$e->getMessage(),'error');
                    return false;
                }

            }else{           
                try {
                    
                    $model->allowField(true)->save($dataum);

                } catch (\Exception $e) {
                    Log::record('off_write表插入数据'.$e->getMessage(),'error');
                    return false;
                }
            }
        } 
    }

    public function renovate($data,$value){
        $cars =Db::name('cars')->where(['car_unique_id'=>$data])->find();
        if($cars){
            $dataum['car_sn'] = $cars['car_sn'];
            $dataum['car_unique_id'] = $data;
            $dataum['brand_id'] = $cars['brand_id'];
            $dataum['agency_id'] = $cars['agency_id'];
            $dataum['equipment_id'] = $cars['equipment_id'];
            $dataum['cardcode'] = $cars['cardcode'];
            $dataum['sim_id'] = $cars['sim_id'];
            $dataum['supplier_no'] = $cars['supplier_no'];
            $dataum['machine_report_time'] = $value['machine_report_time'];
        }
        return $dataum;
    }*/

    public function abnormal(){
        $cars = ['LNBSCU3H7JR059569','LNBSCU3H7JR059507','LNBSCU3HXJR059565','LNBSCU3H2JR059592','LNBSCU3H1JR059566','LJ1EEKPN7H7423905','LNBSCB3F2JW173239','LNBSCB3F8JW173262'];    // 离线车辆

        $data = ['LNBSCU3H7JR059507','LNBSCU3HXJR059565','LJ1EEKPN7H7423905','LNBSCB3F1JW173278'];        // 异常离线表数据

        $dataum = array_diff($data, $cars);             // ===>K不在离线车辆中,即K在线

        dump($dataum);
    }

    public function normal(){
        $cars = ['A','B','C','D','E','F','G','H'];   //离线车辆
        $data = ['D','E','G','I','H'];                          //正常离线表数据
        $dataum = array_diff($data,$cars);                                      // ===>I不在离线车辆中,即I在线
        dump($dataum);
    }

    public function record(){
        $cars = ['A','B','C','D','E','F','G','H'];   //离线车辆
        $data = ['A','E'];                                      //白名单数据
        $dataum = array_diff($cars,$data);                                 // ===>B C D F G H 在离线车辆中,不在白名单中,即为异常离线车辆
        dump($dataum);            
    }

    public function time(){
       try{
            //白名单数据,即安全期内的车辆
            $cars = Db::name('off_record')->where('create_time','exp','>unix_timestamp()-`time`')->column('car_unique_id');

        }catch(\Exception $e){
            Log::record('查找白名单车辆数据'.$e->getMessage(),'error');
        }

        $res = array_intersect($param, $cars);

        dump($res);

    }

    public function test(){
        $cars = Db::name('cars')->where(['supplier_no'=>1376920003])->column('car_unique_id');
        $data = Db::name('cars_info')->where(['supplier_no'=>1376920003])->column('car_unique_id');
        $dataum = array_diff($cars,$data);
        $res = Db::name('cars_temp')->where(['supplier_no'=>1376920003])->column('car_unique_id');
        /*foreach ($dataum as $key => $value) {
            Db::name('cars_info')->where(['car_unique_id'=>$value])->setField('supplier_no',1376920003);
        }*/
        $param = Db::name('off_normal')->select();
        dump(count($cars));
        dump(count($data));
        dump(count($res));
        dump($dataum);
        dump(count($param));

    }
}


/*1533969883*/